package com.example.androidbighomework.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.example.androidbighomework.MainActivity;
import com.example.androidbighomework.R;
import com.example.androidbighomework.SQLiteHelper.MyHelper;

public class AddFragment extends Fragment {

    public static final String ACCOUNT1="account";
    Button mbtn_fabiao;
    EditText met_fabiao;
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable final ViewGroup container, @Nullable Bundle savedInstanceState) {
        //根据xml布局文件来实例化相应的View
        View view=inflater.inflate(R.layout.activity_add_fragment, container, false);
        mbtn_fabiao=view.findViewById(R.id.btn_fabiao);
        met_fabiao=view.findViewById(R.id.et_fabiao);


//        final Integer accountid= ((MainActivity)getActivity()).getAccountId();
//        Log.i("AAAAAAAAAAAAAAAAAID", String.valueOf(accountid));

        mbtn_fabiao.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(((MainActivity)getActivity()).getAccountId()!=null){
                    MyHelper myHelper=new MyHelper(getContext());
                    if(met_fabiao.getText().toString().trim().equals("")){
                        Toast.makeText(getContext(), "不能为空", Toast.LENGTH_SHORT).show();
                    }
                    else{
                        myHelper.insert(((MainActivity)getActivity()).getAccountId(),met_fabiao.getText().toString().trim());
                        met_fabiao.setText("");
                        Toast.makeText(getContext(), "发表成功", Toast.LENGTH_SHORT).show();
                    }

                }else {
                    Toast.makeText(getContext(), "尚未登录,请先登录", Toast.LENGTH_SHORT).show();
                }

            }
        });


        return view;
    }
}